package com.sjb.cp.controller;

import com.sjb.cp.common.BaseResponse;
import com.sjb.cp.common.ResultUtils;
import com.sjb.cp.exception.ErrorCode;
import com.sjb.cp.exception.ThrowUtils;
import com.sjb.cp.model.po.Space;
import com.sjb.cp.model.po.User;
import com.sjb.cp.model.request.analyze.*;
import com.sjb.cp.model.vo.*;
import com.sjb.cp.service.SpaceAnalyzeService;
import com.sjb.cp.service.UserService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author:sjb
 * @CreateTime:2025-02-12
 * @Description: 空间分析请求视图
 * @Version：1.0
 */
@RestController
@RequestMapping("/space/analyze")
public class SpaceAnalyzeController {

    @Resource
    private SpaceAnalyzeService spaceAnalyzeService;

    @Resource
    private UserService userService;

    @PostMapping("/analyze/usageAnalyze")
    public BaseResponse<SpaceUsageAnalyzeVo> getSpaceUsageAnalyze(@RequestBody SpaceAnalyzeRequest spaceAnalyzeRequest,
                                                                  HttpServletRequest request) {
        ThrowUtils.throwIf(spaceAnalyzeRequest == null, ErrorCode.PARAMS_ERROR);
        User loginUser = userService.getLoginUser(request);
        SpaceUsageAnalyzeVo spaceUsageAnalyze = spaceAnalyzeService.getSpaceUsageAnalyze(spaceAnalyzeRequest, loginUser);
        return ResultUtils.success(spaceUsageAnalyze);

    }

    @PostMapping("/analyze/categoryAnalyze")
    public BaseResponse<List<SpaceCategoryAnalyzeVo>> getCategoryAnalyze(@RequestBody SpaceCategoryAnalyzeRequest spaceCategoryAnalyzeRequest,
                                                                   HttpServletRequest request) {
        ThrowUtils.throwIf(spaceCategoryAnalyzeRequest == null, ErrorCode.PARAMS_ERROR);
        User loginUser = userService.getLoginUser(request);
        List<SpaceCategoryAnalyzeVo> spaceCategoryAnalyzeList = spaceAnalyzeService.
                getSpaceCategoryAnalyzeList(spaceCategoryAnalyzeRequest, loginUser);
        return ResultUtils.success(spaceCategoryAnalyzeList);
    }

    @PostMapping("/analyze/tagAnalyze")
    public BaseResponse<List<SpaceTagAnalyzeVo>> getTagAnalyze(@RequestBody SpaceTagAnalyzeRequest spaceTagAnalyzeRequest,
                                                               HttpServletRequest request) {
        ThrowUtils.throwIf(spaceTagAnalyzeRequest == null, ErrorCode.PARAMS_ERROR);
        User loginUser = userService.getLoginUser(request);
        List<SpaceTagAnalyzeVo> spaceTagAnalyzeList = spaceAnalyzeService.
                getSpaceTagAnalyzeList(spaceTagAnalyzeRequest, loginUser);
        return ResultUtils.success(spaceTagAnalyzeList);
    }

    @PostMapping("/analyze/sizeAnalyze")
    public BaseResponse<List<SpaceSizeAnalyzeVo>> getSizeAnalyze(@RequestBody SpaceSizeAnalyzeRequest spaceSizeAnalyzeRequest,
                                                                 HttpServletRequest request){
        ThrowUtils.throwIf(spaceSizeAnalyzeRequest == null, ErrorCode.PARAMS_ERROR);
        User loginUser = userService.getLoginUser(request);
        List<SpaceSizeAnalyzeVo> spaceSizeAnalyze = spaceAnalyzeService.
                getSpaceSizeAnalyze(spaceSizeAnalyzeRequest, loginUser);
        return ResultUtils.success(spaceSizeAnalyze);
    }

    @PostMapping("/analyze/usageAnalyze")
    public BaseResponse<List<SpaceUserAnalyzeVo>> getUsageAnalyze(@RequestBody SpaceUsageAnalyzeRequest spaceUsageAnalyzeRequest,
                                                                  HttpServletRequest request){
        ThrowUtils.throwIf(spaceUsageAnalyzeRequest == null, ErrorCode.PARAMS_ERROR);
        User loginUser = userService.getLoginUser(request);
        List<SpaceUserAnalyzeVo> spaceUserAnalyze = spaceAnalyzeService
                .getSpaceUserAnalyze(spaceUsageAnalyzeRequest, loginUser);
        return ResultUtils.success(spaceUserAnalyze);
    }

    @PostMapping("/analyze/topNAnalyze")
    public BaseResponse<List<Space>> getTopNSpace(@RequestBody SpaceRankAnalyzeRequest spaceRankAnalyzeRequest,
                                                  HttpServletRequest request){
        ThrowUtils.throwIf(spaceRankAnalyzeRequest == null, ErrorCode.PARAMS_ERROR);
        User loginUser = userService.getLoginUser(request);
        List<Space> topSpace = spaceAnalyzeService.getTopSpace(spaceRankAnalyzeRequest, loginUser);
        return ResultUtils.success(topSpace);

    }

}
